﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MINERP.BLL.Statistics;
using MINERP.MODEL.Statistics;
using FastReport;

namespace MINERP.Statistics.Forms.ProfessionalReports
{
    public  partial class FrmInvoice : Form
    {
        Report report =new Report();
        private static  FrmInvoice _instance = null;

        private  InvoiceBLL _bll = new InvoiceBLL();

        public static FrmInvoice Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new FrmInvoice();
                }
                return _instance;
            }
        }

        private FrmInvoice()
        {
            InitializeComponent();
        }

        private void labelControl2_Click(object sender, EventArgs e)
        {

        }

        private void FrmInvoice_Load(object sender, EventArgs e)
        {
            LinkSaleMan();
        }

        /// <summary>
        /// 绑定业务员（总店名）
        /// </summary>
        private void LinkSaleMan()
        {
            cmbSaleman.Properties.Items.Clear();
            List<Salesman_Info> list = _bll.GetSalemanList();
            foreach (var item in list)
            {
                cmbSaleman.Properties.Items.Add(item.SalesmanName);
            }
        }

        private void FrmInvoice_FormClosing(object sender, FormClosingEventArgs e)
        {
            _instance = null;
        }

        private void btnQueryBill_Click(object sender, EventArgs e)
        {
            DateTime start = dtStart.DateTime.Date;
            DateTime end = dtEnd.DateTime.AddDays(1).Date.AddSeconds(-1);
            if (start > end)
            {
                MessageBox.Show("开始时间不能大于结束时间!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }

            if (cmbSaleman.Text.Trim() != string.Empty)
            {
                LinkBillList(cmbSaleman.Text, start, end);
            }
            else
            {
                MessageBox.Show("请选择总店名!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }


        private void LinkBillList(string saleman,DateTime start,DateTime end)
        {
            List<Bill_Info> billList = _bll.GetBillList(saleman, start, end);
            gcBillList.DataSource = billList;
            gcBillList.RefreshDataSource();
        }

        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            List<Bill_Info> billList = (List<Bill_Info>)gcBillList.DataSource;
            foreach (var item in billList)
            {
                item.IsChecked = true; 
            }
            gcBillList.RefreshDataSource();     
        }

        private void btnInverSeselect_Click(object sender, EventArgs e)
        {
            List<Bill_Info> billList = (List<Bill_Info>)gcBillList.DataSource;
            foreach (var item in billList)
            {
                if (item.IsChecked)
                {
                    item.IsChecked = false;
                }
                else
                {
                    item.IsChecked = true;
                }
            }
            gcBillList.RefreshDataSource();     

        }

        private void tsbtnPrintSendGoodsNotice_Click(object sender, EventArgs e)
        {
            if (CheckPrint())
            {
                ShowEMSForm();
                List<Bill_Info> billList = ((List<Bill_Info>)gcBillList.DataSource).Where(b => b.IsChecked == true).ToList();
                if (billList.Count > 0)
                {
                    DataSet dt = _bll.GetSendGoodsNotice(billList.Select(b => b.BillID).ToList());
                    dt.WriteXml(@"C:\WINDOWS\xmlData\SendGoodsNotice.xml", XmlWriteMode.WriteSchema);
               
                    report.RegisterData(dt);
                    report.Load(Application.StartupPath + "\\Report\\SendGoodsNotice.frx");
                    report.SetParameterValue("EMSNumber", Properties.Settings.Default.EMS);
                    report.Show();
                }
                else
                {
                    MessageBox.Show("请选择要打印完成品发送通知的账单!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                MessageBox.Show("请先查询出要打印的账单!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }


        private bool CheckPrint()
        {
            bool flag = false;
            if (gcBillList.DataSource != null)
            {
                flag = true;
            }
            return flag;
        }

        private void tsbtnPrintPackingList_Click(object sender, EventArgs e)
        {
            if (CheckPrint())
            {
                ShowEMSForm();
                List<Bill_Info> billList = ((List<Bill_Info>)gcBillList.DataSource).Where(b => b.IsChecked == true).ToList();
                if (billList.Count > 0)
                {
                    DataSet dt = _bll.GetPackingList(billList.Select(b => b.BillID).ToList());
                    dt.WriteXml(@"C:\WINDOWS\xmlData\PackingList.xml", XmlWriteMode.WriteSchema);

                    report.RegisterData(dt);
                    report.Load(Application.StartupPath + "\\Report\\PackingList.frx");
                    report.SetParameterValue("EMSNumber", Properties.Settings.Default.EMS);
                    report.Show();
                }
                else
                {
                    MessageBox.Show("请选择要打印PackingList的账单!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                MessageBox.Show("请先查询出要打印的账单!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }

        private void tsbtnPrintInvoice_Click(object sender, EventArgs e)
        {
            if (CheckPrint())
            {
                ShowEMSForm();
                List<Bill_Info> billList = ((List<Bill_Info>)gcBillList.DataSource).Where(b => b.IsChecked == true).ToList();
                if (billList.Count > 0)
                {
                    DataSet dt = _bll.GetInvoice(billList.Select(b => b.BillID).ToList());
                    dt.WriteXml(@"C:\WINDOWS\xmlData\Invoice.xml", XmlWriteMode.WriteSchema);

                    report.RegisterData(dt);
                    report.Load(Application.StartupPath + "\\Report\\Invoice.frx");
                    report.SetParameterValue("EMSNumber", Properties.Settings.Default.EMS);
                    report.Show();
                }
                else
                {
                    MessageBox.Show("请选择要打印INVOICE的账单!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                MessageBox.Show("请先查询出要打印的账单!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }


        }

        private void tsbtnRequest_Click(object sender, EventArgs e)
        {
            if (CheckPrint())
            {
                List<Bill_Info> billList = ((List<Bill_Info>)gcBillList.DataSource).Where(b => b.IsChecked == true).ToList();
                if (billList.Count > 0)
                {
                    DataSet dt = _bll.GetRequestPaper(billList.Select(b => b.BillID).ToList());
                    dt.WriteXml(@"C:\WINDOWS\xmlData\RequesePaper.xml", XmlWriteMode.WriteSchema);

                    report.RegisterData(dt);
                    report.Load(Application.StartupPath + "\\Report\\RequesePaper.frx");
                    report.Show();
                }
                else
                {
                    MessageBox.Show("请选择要打印请求书的账单!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                MessageBox.Show("请先查询出要打印的账单!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void gvBillList_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            Bill_Info billInfo = gvBillList.GetFocusedRow() as Bill_Info;
            if (e.Column.FieldName == "IsChecked")
            {
                billInfo.IsChecked = (bool)e.Value;
            }
        }

        private void ShowEMSForm()
        {
            FrmInputEMS frm = new FrmInputEMS();
            frm.ShowDialog();
        }
  
    }
}
